<template>
    <div>
        <h1>门店添加</h1>
        <table class="table table-bordered" border="1px">
            <tbody>
                <tr>
                    <td>门店名称</td>
                    <td>
                        <input type="text" v-model="info.StoreName">
                    </td>
                </tr>
                <tr>
                    <td>分店名称</td>
                    <td>
                        <input type="text" v-model="info.StoresName">
                    </td>
                </tr>
                <tr>
                    <td>商户地址</td>
                    <td>
                        <select v-model="info.Addr">
                            <option value="">请选择</option>
                            <option :value="a.Addr" v-for="a in hhh">{{ a.Addr }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>图片门店</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="info.Sotreimg" width="120px" height="100px" v-if="info.Sotreimg">
                    </td>
                </tr>
                <tr>
                    <td>联系电话</td>
                    <td>
                        <input type="text" v-model="info.StorePhone">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="info.State" :value="true"> 启用
                        <input type="radio" v-model="info.State" :value="false"> 停用
                    </td>
                </tr>
                <tr>
                    <td>开业日期</td>
                    <td>
                        <input type="date" v-model="info.Createtime">
                    </td>
                </tr>
                <tr>
                    <td>门店介绍</td>
                    <td>
                        <textarea v-model="info.Storecontent"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>入住须知</td>
                    <td>
                        <textarea v-model="info.StoreNow"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>交通信息</td>
                    <td>
                        <textarea v-model="info.Storetext"></textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button value="提交" class="btn btn-primary" @click="ok">提交</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router'
const router = useRouter();

let hhh: any = ref([])

let info: any = reactive({
    "StoreName": "",
    "StoresName": "",
    "Addr": "",
    "Sotreimg": "",
    "StorePhone": "",
    "State": true,
    "Createtime": "",
    "Storecontent": "",
    "StoreNow": "",
    "Storetext": "",
})

onMounted(() => {
    bind();
})

const bind = () => {
    axios({
        url: 'https://localhost:7056/api/Store/BindAddr',
        method: 'get',
    })
        .then(res => {
            console.log(res.data);
            Object.assign(hhh.value, res.data);
        })
}

const img = (e: any) => {
    console.log(e);
    let obj = e.target.files[0];
    let f = new FormData;
    f.append('file', obj);
    axios({
        url: 'https://localhost:7056/img',
        method: 'post',
        data: f
    })
        .then((res) => {
            info.Sotreimg = res.data;
        })
}

const ok = () => {
    if (info.StoreName == "") {
        alert('门店名称不能为空!');
        return;
    }
    if (info.StoresName == "") {
        alert('分店名称不能为空!');
        return;
    }
    if (info.Addr == "") {
        alert('商户地址不能为空!');
        return;
    }
    if (info.Sotreimg == "") {
        alert('图片门店不能为空!');
        return;
    }
    if (info.StorePhone == "") {
        alert('联系电话不能为空!');
        return;
    }
    if (info.Createtime == "") {
        alert('开业日期不能为空!');
        return;
    }
    if (info.Storecontent == "") {
        alert('门店介绍不能为空!');
        return;
    }
    if (info.StoreNow == "") {
        alert('入住须知不能为空!');
        return;
    }
    if (info.Storetext == "") {
        alert('交通信息不能为空!');
        return;
    }
    axios({
        url: 'https://localhost:7056/api/Store/AddStore',
        method: 'post',
        data: info
    })
        .then(res => {
            if (res.data == -1) {
                alert('名称重复!');
                return;
            }
            else if (res.data > 0) {
                alert('添加成功!');
                router.push('/SShow');
                return;
            }
            else {
                alert('添加失败!');
                return;
            }
        })
}
</script>

<style scoped></style>